const express = require("express");
const router = express.Router();
const axios = require('axios')
const db = require('../../db/index.js')

router.get("/person/basic/data", async (req, res) => {
    try {
        const url = `https://www.gate.com/zh/strategybot/user_profile?name=${req.query.name}`;
        const response = await axios.get(url);
        const data = response.data.data.profile;
        data.labels = JSON.stringify(data.labels)
        data.subscribes_trading_get = JSON.stringify(data.subscribes_trading_get)

        const selectSql = 'SELECT * FROM user_table WHERE anonymous_uid = ?'
        // 查询数据库中是否存在
        db.query(selectSql, [req.query.name], (err, results) => {
            if (results.length !== 1) {
                let insertSql = 'INSERT INTO user_table SET ?'
                db.query(insertSql, data, (err, inserResults) => {
                    if (err) return res.fail(err)
                    if (inserResults.affectedRows !== 1) return res.fail('同步失败！')
                    data.labels = JSON.parse(data.labels)
                    data.subscribes_trading_get = JSON.parse(data.subscribes_trading_get)
                    res.success({ data: response.data.data, message: "同步成功！" });
                });
            }
            if (results.length === 1) {
                let updateSql = 'UPDATE user_table SET ? WHERE anonymous_uid = ?'
                db.query(updateSql, [data, req.query.name], (err, updateResults) => {
                    if (err) return res.fail(err)
                    if (updateResults.affectedRows !== 1) return res.fail('同步失败！')
                    data.labels = JSON.parse(data.labels)
                    data.subscribes_trading_get = JSON.parse(data.subscribes_trading_get)
                    res.success({ data: response.data.data, message: "同步成功！" });
                });
            }
        });
    } catch (error) {
        console.error(error);
        res.fail(error)
    }
});

router.get("/basic/main/data", async (req, res) => {
    try {

        const selectSql = 'SELECT * FROM user_table WHERE anonymous_uid = ?'
        db.query(selectSql, [req.query.name], (err, results) => {

            if (err) return res.fail(err)

            if (results.length == 1) {
                results[0].labels = JSON.parse(results[0].labels)
                results[0].subscribes_trading_get = JSON.parse(results[0].subscribes_trading_get)
                res.success({ data: results[0], message: "查询成功！" });
            } else {
                res.success({ message: "查询失败！" });
            }
        });
    } catch (error) {
        console.error(error);
        res.fail(error)
    }
});

router.post("/person/upload/info", (req, res) => {
    console.log(req.body);

    let sql = 'UPDATE user_table SET ? WHERE anonymous_uid = ?'
    req.body.labels = JSON.stringify(req.body.labels)
    req.body.subscribes_trading_get = JSON.stringify(req.body.subscribes_trading_get)
    db.query(sql, [req.body, req.body.anonymous_uid], (err, results) => {
        if (err) return res.fail(err)
        if (results.affectedRows !== 1) return res.fail('上传失败！')
        res.success({ data: req.data, message: "上传成功！" });
    });
})


module.exports = router;